package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import apps.Info;

/**
 * Servlet implementation class LoginHandler
 */
public class LoginHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * import apps.PutInfo;
	 * 
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginHandler() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user, pass;
		HttpSession session = request.getSession();

		user = request.getParameter("user");
		pass = request.getParameter("pass");

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		out.println("<HTML><HEAD><TITLE>Login</TITLE></HEAD><BODY>");
		out.println("<H1>Hello " + user + " We're trying to log you in... "
				+ "</H1>");
		out.println("</BODY></HTML>");

		if ((user != null)&& (user.length() > 0)) {
			session.setAttribute("use", user);
			System.out.println("here -"+user.length());
		}

		if (Info.doLogin(user, pass) || session.getAttribute("use") != null ) {
			session.setAttribute("info.value", Info.getInfo(session
					.getAttribute("use").toString()));
			session.setAttribute("month", "-1");
			session.setAttribute("year", "-1");
			response.sendRedirect("Calendar.jsp");
			//System.out.println("here in-"+user.length());
		} else {
			response.sendRedirect("password_error.html");
		}
		out.close();

	}

}
